package cc.market.config;

import cc.market.interceptor.JwtTokenAdminInterceptor;
import cc.market.interceptor.JwtTokenUserInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 配置类，注册web层相关组件
 */
@Configuration
@Slf4j
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private JwtTokenAdminInterceptor jwtTokenAdminInterceptor;
    @Autowired
    private JwtTokenUserInterceptor jwtTokenUserInterceptor;

    /**
     * 注册自定义拦截器
     * @param registry
     */
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("开始注册自定义拦截器...");
        registry.addInterceptor(jwtTokenAdminInterceptor)
                .addPathPatterns("/admin/**")
                .excludePathPatterns("/admin/login");
        registry.addInterceptor(jwtTokenUserInterceptor)
                .addPathPatterns("/user/**") // 拦截/user后面的所有路径，除了登录和状态请求
                .excludePathPatterns("/user/product/getcategory") //获取分类不拦截
                .excludePathPatterns("/user/product/getprouctbyid")//获取商品预览不拦截
                .excludePathPatterns("/user/product/getbycategoryid")//获取商品预览不拦截
                .excludePathPatterns("/user/product/getproductdetail")//获取商品详情不拦截
                .excludePathPatterns("/user/login");
    }
}
